//
//  AXFTransitioningAnimator.m
//  AXFEightGroup
//
//  Created by ya on 12/29/16.
//  Copyright © 2016 ya. All rights reserved.
//

#import "AXFTransitioningAnimator.h"

@interface AXFTransitioningAnimator ()<UIViewControllerAnimatedTransitioning>
@property (nonatomic, assign, getter=isPresented) BOOL presented;
@property (nonatomic, weak) id<UIViewControllerContextTransitioning> transitionContext;
@end

@implementation AXFTransitioningAnimator

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    self.presented = YES;
    return self;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    self.presented = NO;
    return self;
}


























- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.25;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    self.isPresented == YES ? [self presentedAnimation:transitionContext] : [self dismissedAnimation:transitionContext];
}

- (void)presentedAnimation:(id<UIViewControllerContextTransitioning>)transitionContext {
    self.transitionContext = transitionContext;
    UIView *presentedView = [transitionContext viewForKey:UITransitionContextToViewKey];
    UIView *containerView = [transitionContext containerView];
    containerView.backgroundColor = [UIColor clearColor];
    [containerView addSubview:presentedView];
    
    presentedView.alpha = 0.0001;
//    presentedView.transform = CGAffineTransformMakeScale(1, 0.0001);
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        presentedView.alpha = 1.0;
//        presentedView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];

}


- (void)dismissedAnimation:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIView *dismissedView = [transitionContext viewForKey:UITransitionContextFromViewKey];
//    dismissedView.layer.anchorPoint = CGPointMake(0.5, 0);
    dismissedView.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        dismissedView.transform = CGAffineTransformMakeScale(1, 0.00001);
//        dismissedView.transform = CGAffineTransformMakeScale(1.0, 0.00001);
    } completion:^(BOOL finished) {
        [dismissedView removeFromSuperview];
        [transitionContext completeTransition:YES];
    }];
}


@end
